package com.audaque.springboot.foshanupload.web.webdemo.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zgb
 * @desc ...
 * @date 2022-05-17 16:08:59
 */

@Slf4j
@RestController
@RequestMapping("itextpdf")
public class ItextpdfController {
    private static final String DEST1 = "E:\\huaAo\\code\\myDiy\\脚手架\\单体\\foshanupload\\demo\\target\\HelloWorld.pdf";
    private static final String DEST2 = "E:\\huaAo\\code\\myDiy\\脚手架\\单体\\foshanupload\\demo\\target\\HelloWorld_CN.pdf";
    private static final String DEST3 = "E:\\huaAo\\code\\myDiy\\脚手架\\单体\\foshanupload\\demo\\target\\HelloWorld_CN_HTML.pdf";
    private static final String DEST4 = "E:\\huaAo\\code\\myDiy\\脚手架\\单体\\foshanupload\\demo\\target\\HelloWorld_CN_HTML_FREEMARKER.pdf";
    private static final String DEST5 = "E:\\huaAo\\code\\myDiy\\脚手架\\单体\\foshanupload\\demo\\target\\HelloWorld_CN_HTML_FREEMARKER_FS.pdf";

/*
    @GetMapping("test1")
    public void test1() throws Exception {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST1));
        document.open();
        document.add(new Paragraph("hello world"));
        document.close();
        writer.close();

    }

    @GetMapping("test2")
    public void test2() throws Exception {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST2));
        document.open();

        ClassPathResource classPathResource = new ClassPathResource(ItextpdfUtils.FONT_CLASSPATH);
        String fontPath = classPathResource.getPath();
        log.debug("fontPath:" + fontPath);
        Font f1 = FontFactory.getFont(ItextpdfUtils.FONT_CLASSPATH, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        document.add(new Paragraph("hello world,我是鲁家宁", f1));
        document.close();
        writer.close();
    }


    @GetMapping("test3")
    public void test3() throws Exception {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST3));
        document.open();

        ClassPathResource classPathResource1 = new ClassPathResource(ItextpdfUtils.FONT_CLASSPATH);
        String fontPath = classPathResource1.getPath();

        ClassPathResource classPathResource2 = new ClassPathResource("html/index.html");
        InputStream inputStream = classPathResource2.getInputStream();


        XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
        fontImp.register(fontPath);
        XMLWorkerHelper.getInstance().parseXHtml(writer, document, inputStream, null, Charset.forName("UTF-8"), fontImp);


        document.close();
        writer.close();
    }

    @GetMapping("test4")
    public void test4() throws Exception {
        Map<String, Object> data = new HashMap();
        //data.put("name", "鲁家宁");
        String content = ItextpdfUtils.freeMarkerRender(data, "html/template_freemarker.html");
        ItextpdfUtils.createPdf(content, DEST4);


    }

    @GetMapping("test5")
    public void test5() throws Exception {
        Map<String, Object> data = new HashMap();
        data.put("name", "鲁家宁");
        String content = ItextpdfUtils.freeMarkerRender(data, "html/template_freemarker_fs.html");
        ItextpdfUtils.createPdfWithImg(content, DEST5,"logo.png");
    }*/
}
